package com.demo2do.fms.admin.service.supplier.impl;

import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.supplier.SupplierDao;
import com.demo2do.fms.admin.dao.supplier.SupplierProfileDao;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.supplier.SupplierInfoService;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.SupplierEvent;
import com.demo2do.fms.entity.supplier.common.SupplierEventType;
import com.demo2do.fms.entity.supplier.common.SupplierInfo;
import com.demo2do.fms.entity.supplier.common.SupplierModifiedVersion;
import com.demo2do.fms.entity.supplier.common.SyncStatus;
import com.demo2do.fms.entity.vehicle.ModifiedStatus;

/**
 * 
 * @author Wy
 *
 */
@Service("supplierInfoService")
public class SupplierInfoServiceImpl extends DataServiceImpl<SupplierInfo> implements SupplierInfoService {

    @Dao
    private SupplierProfileDao supplierInfoDao;
    
    @Dao
    private SupplierDao supplierDao;

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierInfoService#load(java.lang.Long)
     */
    public SupplierInfo load(Long supplierId) {
        return supplierInfoDao.getSupplierInfo(supplierId);
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierInfoService#edit(com.demo2do.fms.entity.supplier.common.Supplier, com.demo2do.fms.entity.supplier.common.SupplierInfo, java.lang.Long)
     */
    public void edit(Supplier supplier , SupplierInfo supplierInfo, Long operator) {
        
        // 更新供方
        genericDaoSupport.update(supplier.operate(operator));
        
        // 更新供方基本信息
        genericDaoSupport.update(supplierInfo.operate(operator));
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierInfoService#prepareChange(com.demo2do.fms.entity.supplier.common.Supplier)
     */
    public SupplierModifiedVersion prepareChange(Supplier supplier) {
        
        // save supplier modified version
        SupplierModifiedVersion lastedVersion = supplierDao.loadModifiedVersion(supplier.getId(), ModifiedStatus.AUDITING);
        int version = lastedVersion == null ? 1 : lastedVersion.getVersionSorting() + 1;
        
        SupplierModifiedVersion supplierModifiedVersion = new SupplierModifiedVersion(supplier, version);
        genericDaoSupport.save(supplierModifiedVersion);
        
        return supplierModifiedVersion;
        
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierInfoService#change(com.demo2do.fms.entity.supplier.common.Supplier, com.demo2do.fms.entity.supplier.common.SupplierModifiedVersion, org.springframework.web.multipart.MultipartFile, org.springframework.web.multipart.MultipartFile, org.springframework.web.multipart.MultipartFile, org.springframework.web.multipart.MultipartFile, java.lang.Long)
     */
    public void change(Supplier supplier, SupplierModifiedVersion supplierModifiedVersion, Long operator) {
        
        // 1. update supplier status from QUALIFIED to CHANGEING
        genericDaoSupport.update(supplier.editSyncStatus(SyncStatus.MODIFIED, operator));
        
        // 2. save supplier modified version
        genericDaoSupport.update(supplierModifiedVersion);
        
        // 3. save supplier_event
        genericDaoSupport.save(new SupplierEvent(supplier, SupplierEventType.CHANGE, operator));

    }

    /**
     * 更新付款信息
     *  @param supplierId
     * @param operator
     * @param supplierInfo
     */
    public void updatePaymentInfo(Long supplierId, Long operator, SupplierInfo supplierInfo) {

        SupplierInfo load = supplierInfoDao.getSupplierInfo(supplierId);

        load.updatePaymentInfo(supplierInfo);

        load.operate(operator);

        genericDaoSupport.update(load);
    }

}
